package com.guli.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.eduservice.bean.EduComment;
import com.guli.eduservice.bean.comment.OneComment;
import com.guli.eduservice.mapper.EduCommentMapper;
import com.guli.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-19
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {
    @Override
    public Map<String, Object> getCommentById(String courseId,long page,long limit) {

        List<OneComment> oneComment = findOneComment(courseId);//一级评论
        List<OneComment> oneComments = dfsComment(oneComment, courseId);//所有评论都封装成为了OneComment

        //对查询出来的内容进行分页显示
        //当前页
        long current = page;
        //每页数据条数
        long size = limit;
        int count = 0;
        Page<OneComment> pageParam = new Page<>(current,size);
        if(oneComments != null){
           count = oneComments.size();
        }

        List<OneComment> pageList = new ArrayList<>();
        //计算当前页第一条数据的下标
        long currId = current > 1 ? (current-1) * size : 0;
        for (long i = 0; i < size && i < count - currId;i++){
            pageList.add(oneComments.get((int)(currId+i)));
        }
        pageParam.setSize(size);
        pageParam.setCurrent(current);
        pageParam.setTotal(count);
        //计算分页总页数
        pageParam.setPages(count % size == 0 ? count / size : count / size + 1);
        pageParam.setRecords(pageList);

        Map<String, Object> map = new HashMap<>();
        map.put("items", pageList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return map;
    }

    //递归查找一级分类下的所有评论
    public List<OneComment> dfsComment(List<OneComment> fatherComments,String courseId){
        if(fatherComments == null || fatherComments.size() == 0){
            return null;
        }
        for (OneComment oneComment : fatherComments) {
            String fatherId = oneComment.getId();
            //查询上级评论下的所有评论
            List<OneComment> comments = findComment(courseId, fatherId);
            oneComment.setChildren(dfsComment(comments,courseId));
        }
        return fatherComments;
    }
    //查询所有一级评论
    public List<OneComment> findOneComment(String courseId){
        //查询一级评论
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId).eq("father_id","0").orderByAsc("gmt_create");
        List<EduComment> oneComments = baseMapper.selectList(wrapper);
        List<OneComment> arrayList = new ArrayList<>();
        for (EduComment eduComment : oneComments) {
            OneComment oneComment = new OneComment();
            BeanUtils.copyProperties(eduComment,oneComment);
            arrayList.add(oneComment);
        }
        return arrayList;
    }
    //查询上级评论下的所有评论
    public List<OneComment> findComment(String courseId,String fatherId){
        QueryWrapper<EduComment> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id",courseId).eq("father_id",fatherId).orderByAsc("gmt_create");
        //得到父级评论下的子级评论
        List<EduComment> eduComments = baseMapper.selectList(wrapper1);
        List<OneComment> arrayList = new ArrayList<>();
        for (EduComment eduComment : eduComments) {
            OneComment oneComment = new OneComment();
            BeanUtils.copyProperties(eduComment,oneComment);
            arrayList.add(oneComment);
        }
        return arrayList;
    }
}
